
package com.ziyun.pop.web.modules.storeMember.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.netflix.discovery.converters.Auto;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.remittance.entity.RemittanceEntity;
import com.ziyun.pop.modules.remittance.service.RemittanceService;
import com.ziyun.pop.modules.storeMember.entity.StoreMemberEntity;
import com.ziyun.pop.modules.storeMember.service.StoreMemberService;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;


/**
 * 门店会员关联表
 *
 * @author jiangli
 * @email
 * @date 2018-09-21 15:47:40
 */

@RestController
@RequestMapping("storeMember/storemember")
public class StoreMemberController {
    @Autowired
    private StoreMemberService storeMemberService;
    @Autowired
    private RemittanceService remittanceService;


    /**
     * 列表
     */

    @RequestMapping("/list")
    @RequiresPermissions("storeMember:storemember:list")
    public R list(@RequestParam Map<String, Object> params) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        params.put("sellerId", sellerId);
        return storeMemberService.list(params);
    }

    @RequestMapping("/twblist")
    public R twblist(@RequestBody Map<String, Object> params) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        params.put("sellerId", sellerId);
        return storeMemberService.list(params);
    }



    /**
     * 信息
     */

    @RequestMapping("/info/{sid}")
    @RequiresPermissions("storeMember:storemember:info")
    public R info(@PathVariable("sid") Long sid) {
        return storeMemberService.getOneById(sid);
    }


    /**
     * 保存
     */

    @RequestMapping("/save")
    @RequiresPermissions("storeMember:storemember:save")
    public R save(@RequestBody StoreMemberEntity storeMember) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        storeMember.setSellerId(sellerId.intValue());
        return storeMemberService.insertStoreMember(storeMember);

    }
    @RequestMapping("/twbsave")
    public R twbsave(@RequestBody StoreMemberEntity storeMember) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        storeMember.setSellerId(sellerId.intValue());
        return  storeMemberService.twbsave(storeMember);
    }


    /**
     * 修改
     */

    @RequestMapping("/update")
    @RequiresPermissions("storeMember:storemember:update")
    public R update(@RequestBody StoreMemberEntity storeMember) {
        storeMemberService.updateById(storeMember);
        //如果价格账套参数为null或"",表示取消价格账套
        if (StringUtils.isEmpty(storeMember.getPriceSet())) {
            storeMemberService.cancel(storeMember.getSid());
        }
        return R.ok();
    }


    /**
     * 删除
     */

    @RequestMapping("/delete")
    @RequiresPermissions("storeMember:storemember:delete")
    public R delete(@RequestBody Long[] sids) {
      return storeMemberService.logicDelete(Arrays.asList(sids));
    }

    /**
     * 预存款充值
     */
    @RequestMapping("/addAdvance")
    @RequiresPermissions("storeMember:storemember:addAdvance")
    public R addAdvance(Integer memberId, BigDecimal advance,String payWay) {
        storeMemberService.addAdvance(memberId,advance,payWay);
        return R.ok();
    }

    //开始扫码充值
    @RequestMapping("/sweepCodeBegin")
    public R sweepCodeBegin(@RequestBody Map<String,Object> params ) {
        return storeMemberService.sweepCodeBegin(params);
    }

    //扫码支付 回调
    @RequestMapping("/sweepCodeCallBack")
    public R sweepCodeCallBack(@RequestBody Map<String,Object> params ) {
        return storeMemberService.sweepCodeCallBack(params);
    }

    //获取银行卡信息
    @RequestMapping("bankCard")
    public R bankCard() {
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        RemittanceEntity remittanceEntity = remittanceService.getOne(new QueryWrapper<RemittanceEntity>().eq("seller_id", sellerId));
        return R.ok().put("remittanceEntity",remittanceEntity);
    }

    //设置银行卡
    @RequestMapping("bindBankCard")
    public R bindBankCard(@RequestBody RemittanceEntity remittanceEntity) {
        remittanceEntity.setSellerId(UserUtils.getCurentUser().getSellerId().intValue());
        return storeMemberService.bindBankCard(remittanceEntity);
    }

}

